package com.leo.leetcode.s100;

/**
 *
 * @author xuexiaolei
 * @version 2017年11月17日
 */
public class T07ReverseInteger {
    public static int reverse(int x) {
        boolean isNegative = false;
        long xlong = x;
        if (xlong < 0){ isNegative = true; xlong=-xlong;}
        char[] cs = String.valueOf(xlong).toCharArray();
        for (int i = 0; i < cs.length/2; i++) {
            char tmp = cs[i];
            cs[i] = cs[cs.length-1-i];
            cs[cs.length-1-i] = tmp;
        }

        xlong = Long.parseLong(String.valueOf(cs));
        xlong = isNegative?-xlong:xlong;//加上负号
        if (xlong >= Integer.MIN_VALUE && xlong <= Integer.MAX_VALUE){
            return (int) xlong;
        }else {
            return 0;
        }
    }

    public static void main(String[] args) {
        System.out.println(reverse(-123));
        System.out.println(reverse(123));
        System.out.println(reverse(120));
        System.out.println(reverse(Integer.MIN_VALUE));
        System.out.println(reverse(Integer.MAX_VALUE));
        System.out.println(reverse(-1234567812));
        System.out.println(reverse(2147483647));
        System.out.println(reverse(-2147483648));
    }
}
